﻿using System.Collections.Generic;
using com.huigo.Framework.Model;
using com.loto.Common.DBUtility;
using System.Data.SqlClient;
using System.Data;

namespace com.huigo.Framework.BLL
{
    public class InsCompany
    {
        public static List<Model.InsCompany> GetInsCompanyDropList()
        {
            List<string> fields = new List<string>()
            {
                "COMPANY_ID",
                "COMPANY_SHORT_NAME"
            };
            List<WhereStatement> whereList = new List<WhereStatement>()
            {
                new WhereStatement("COMPANY_STATUS","=",((int)Dict.IsActive.Yes).ToString(),"")
            };
            List<OrderByStatement> orderList = new List<OrderByStatement>()
            {
                new OrderByStatement("COMPANY_SHORT_NAME",true)
            };
            return SqlServerDal.InsCompany.GetItems(fields, whereList, orderList, null);
        }
        //查询保险公司
        public static List<Model.InsCompany> GetInsCompany( int pageSize, int pageIndex)
        {
            List<string> field = new List<string>() { "COMPANY_ID,COMPANY_NAME,COMPANY_SHORT_NAME,COMPANY_LOGO,COMPANY_WEBSITE, COMPANY_EX ,COMPANY_STATUS " };
            List<WhereStatement> where = new List<WhereStatement>();
            where.Add(new WhereStatement("COMPANY_STATUS", "=", "1", ""));
            List<OrderByStatement> orderList = new List<OrderByStatement>()
            {
                new OrderByStatement("COMPANY_SHORT_NAME",true),
                 new OrderByStatement("COMPANY_STATUS",false)
            };
            List<Model.InsCompany> insList = null;
            int recordCount = 0;
            int pageCount = 0;
            insList = SqlServerDal.InsCompany.GetItemsByProc(field, null, where, orderList, "[PROC_PAGINATION_OUT]", pageSize, pageIndex, out recordCount, out pageCount, null);
            insList.ForEach(delegate(Model.InsCompany info)
            {
                info.RecordCount = recordCount;
                info.PageCount = pageCount;
            });
            return insList;
        }
        //添加保险公司信息
        public static bool AddInsCompany(Model.InsCompany InsInfo)
        {
            return SqlServerDal.InsCompany.Insert(InsInfo, null);
        }
        //根据保险公司id查询保险公司信息
        public static Model .InsCompany GetInsCompanyByID(long insCompanyID)
        {
            Model.InsCompany insInfo = new  Model.InsCompany();
            insInfo.CompanyId = insCompanyID;
            List<string> fields = new List<string>() 
             {
               "COMPANY_ID,COMPANY_NAME,COMPANY_SHORT_NAME,COMPANY_LOGO,COMPANY_WEBSITE, COMPANY_EX ,COMPANY_STATUS "
             };
            return SqlServerDal.InsCompany.GetItem(insInfo, fields, null, null);
        }
        //验证是否有添加过保险公司信息
        public static bool GetInsCompanyList(long companyID,string insName, string insMeno, string insEX)
        {
            bool isExist = false;
            List<string> fields = new List<string>()
            {
                "COMPANY_ID",
                "COMPANY_SHORT_NAME"
            };
            List<SqlParameter> paraList = new List<SqlParameter>();
            SqlParameter para = new SqlParameter("@COMPANY_NAME", SqlDbType.NVarChar, 30);
            para.Value = insName;
            paraList.Add(para);

            para = new SqlParameter("@COMPANY_SHORT_NAME", SqlDbType.NVarChar, 10);
            para.Value = insMeno;
            paraList.Add(para);

            para = new SqlParameter("@COMPANY_EX", SqlDbType.NVarChar, 4);
            para.Value = insEX;
            paraList.Add(para);

            List<WhereStatement> whereList = new List<WhereStatement>();
            whereList.Add(new WhereStatement("COMPANY_STATUS", "=", "1", " and "));
            whereList.Add(new WhereStatement("COMPANY_NAME", "=", "@COMPANY_NAME or COMPANY_SHORT_NAME=@COMPANY_SHORT_NAME or COMPANY_EX =@COMPANY_EX", ""));
           
            List<OrderByStatement> orderList = new List<OrderByStatement>()
            {
                new OrderByStatement("COMPANY_SHORT_NAME",true)
            };
            List<Model.InsCompany> insList= SqlServerDal.InsCompany.GetItems(fields,null, whereList, orderList, paraList.ToArray(),null);
            if (insList != null && insList.Count > 0)
            {
                if (companyID == long.MinValue)
                    isExist = true;
                else
                {
                    foreach (Model.InsCompany c in insList)
                    {
                        if (companyID != c.CompanyId)
                            isExist = true;
                    }
                }
            }
            return isExist;
        }
        //更新保险公司
        public static bool UpdateInsCompanyInfo(long sid, Model.InsCompany insInfo)
        {
            List<WhereStatement> whereList = new List<WhereStatement>()
            {
                    new WhereStatement("COMPANY_ID","=",sid.ToString(),"")
            };
            return SqlServerDal.InsCompany.Update(insInfo, whereList, null);
        }
    }
}
